package com.lan.iot;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

public class Music extends AppCompatActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {
    private void setAddressSelectorPopup(View v) {
        int screenHeigh = getResources().getDisplayMetrics().heightPixels;

        CommonPopWindow.newBuilder()
                .setView(R.layout.timese)
                .setBackgroundDrawable(new BitmapDrawable())
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, Math.round(screenHeigh * 0.3f))
                .setViewOnClickListener(this)
                .setBackgroundDarkEnable(true)
                .setBackgroundAlpha(0.7f)
                .setBackgroundDrawable(new ColorDrawable(999999))
                .build(this)
                .showAsBottom(v);
    }



    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.timese:
                TextView imageBtn = view.findViewById(R.id.img_guanbi);
                PickerScrollView addressSelector = view.findViewById(R.id.address);

                // 设置数据，默认选择第一条
                addressSelector.setData(datasBeanList);
                addressSelector.setSelected(0);

                //滚动监听
                addressSelector.setOnSelectListener(new PickerScrollView.onSelectListener() {
                    @Override
                    public void onSelect(GetConfigReq.DatasBean pickers) {
                        categoryName = pickers.getCategoryName();
                    }
                });

                //完成按钮
                imageBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        text.setText("定时："+categoryName);
                    }
                });
                break;
        }
    }
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
//                setAddressSelectorPopup(v);
                break;
        }
    }
    Switch sw;
    @Override
    protected void onStop() {
        super.onStop();

        Intent intent2 = new Intent("android.intent.action.CART_BROADCAST_m");
        intent2.putExtra("musicdata",sw.isChecked());
        LocalBroadcastManager.getInstance(Music.this).sendBroadcast(intent2);
        sendBroadcast(intent2);    }
    private void initView() {
        click = findViewById(R.id.text);
    }
    private void initListener() {
        click.setOnClickListener(this);
    }
    private TextView click;
    private TextView text;
    private List<GetConfigReq.DatasBean> datasBeanList;
    private String categoryName;
    private void initData() {
        //模拟请求后台返回数据
        String response = "{\"ret\":0,\"msg\":\"succes,\",\"datas\":[{\"ID\":\"  0\",\"categoryName\":\"0.5小时\",\"state\":\"1\"},{\"ID\":\"1\",\"categoryName\":\"1小时\",\"state\":\"1\"},{\"ID\":\"2\",\"categoryName\":\"1.5小时\",\"state\":\"1\"},{\"ID\":\"3\",\"categoryName\":\"2小时\",\"state\":\"1\"}]}";
        GetConfigReq getConfigReq = new Gson().fromJson(response, GetConfigReq.class);
        //0请求表示成功
        if (getConfigReq.getRet() == 0) {
            //滚动选择数据集合
            datasBeanList = getConfigReq.getDatas();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_music);
        sw=findViewById(R.id.sw);
        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        boolean isOpen = bundle.getBoolean("name", true);
        if(isOpen)sw.setChecked(true);
        Button btn=findViewById(R.id.btn);
        initView();
        initData();
        initListener();
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                final Dialog dlg;
//                final Drawable dr = getResources().getDrawable(R.drawable.radius_border_d);
//                dlg = new AlertDialog.Builder(Music.this).create();
//                dlg.show();//显示对话框
//                Window window = dlg.getWindow();//获取对话框窗口\
//                window.setGravity(Gravity.BOTTOM);//设置对话框显示剧中的位置
//                window.setContentView(R.layout.timese);//设置对话框布局
//                window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//
//                window.setBackgroundDrawable(dr);
//                WindowManager.LayoutParams params = dlg.getWindow()
//                        .getAttributes();
////                Display display = getWindowManager().getDefaultDisplay();
////                params.width = (int) (dpToPx(344));
////                        params.width=dpToPx(360);
////                params.dimAmount = 0.0f;
//                dlg.getWindow().setAttributes(params);
//                Button btn1=window.findViewById(R.id.no);
//                btn1.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        dlg.cancel();
//                    }
//                });
//                Button btn2=window.findViewById(R.id.yes);
//                btn2.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        dlg.cancel();
//                    }
//                });
//            }
//        });
        final TextView t=findViewById(R.id.text);
        CircleControlView  view = findViewById(R.id.c);
        view.mMaxProgress=100;
        view.mMinProgress=0;
        view.setOnTextFinishListener(new CircleControlView.OnCirqueProgressChangeListener() {
            @Override
            public void onChange(int minProgress, int maxProgress, int progress) {
//                int prog = progress;
                String s= String.valueOf(progress);
                t.setText("音量："+s);
            }

            @Override
            public void onChangeEnd(int minProgress, int maxProgress, int progress) {
                int end = progress;

            }
        });
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
